/*
 * affine_matching.cc
 *
 *  Created on: Oct 22, 2011
 *      Author: tqlong
 */

#include "affine_matching.h"

void NaiveAffineMatching::match(const FeatureList& lList, const FeatureList& rList)
{
  lList_ = lList;
  rList_ = rList;

  int max_match = 0;

  // RANDSAC
  int max_iter = 500000;
  for (int iter = 0; iter < max_iter; iter++)
  {
    cv::Point2f src[3], dst[3];

    // randomization
    for (int i = 0; i < 3; i++)
    {
      int src_idx = rand() % lList_.size(), dst_idx = rand() % rList_.size();
      src[i] = lList_[src_idx].point2d();
      dst[i] = rList_[dst_idx].point2d();
    }

    cv::Mat transform = cv::getAffineTransform(src, dst);
    int match = count_match(transform);
    if (iter % 10000 == 0)
      std::cerr << iter << " (" << max_match << ") ";
    if (match > max_match)
    {
      max_match = match;
      max_transform_ = transform;
    }
  }
  std::cerr << std::endl;

  if (max_match > 0)
    do_match(max_transform_);
}
